package com.tangruojie.service.impl;

import com.tangruojie.constant.RedisConstant;
import com.tangruojie.service.LogoutService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class LogoutServiceImpl implements LogoutService {

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public void logout() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
        stringRedisTemplate.delete(RedisConstant.USER_TOKEN + userDetails.getUser().getId());
        SecurityContextHolder.clearContext();
    }

}
